﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FaceRecognition.DataBase;


namespace FaceRecognition.Controller
{
    class Accounts_manager
    {
        public void add_account(Int32 uID,string url,string username,string password)
        {

            Account new_account = new Account();
            new_account.UID = uID;
            new_account.URL = url;
            new_account.User_Name = username;
            new_account.Password = password;
            try
            {

                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
                    db.Accounts.InsertOnSubmit(new_account);
                    db.SubmitChanges();
                }

            }
            catch (Exception e)
            {
                throw e;
            }
        }


        public List<string> retreive_all_Url(Int32 uID)
        {
            List<String> URLS = new List<string>();

            try
            {
                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
                    var accounts = from n in db.Accounts
                                  where n.UID == uID //
                                  select n;
                    if (accounts != null)
                    {
                        foreach (var n in accounts)
                        {
                            Account temp = n;
                            URLS.Add(temp.URL);
                        }
                    }
                    return URLS;
                }
            }
            catch (Exception e)
            {
             
                throw e;
            }
        }

        public string retrieve_username(Int32 uID, string url)
        {
            try
            {
                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
                   Account a = (from u in db.Accounts
                            where u.URL == url && u.UID==uID
                            select u).Single();
                    return a.User_Name;
                }

            }
            catch (Exception e)
            {
              
                throw e;
            }
        }
        public string retrieve_password(Int32 uID, string url)
        {
            try
            {
                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
                    Account a = (from u in db.Accounts
                                 where u.URL == url && u.UID == uID
                                 select u).Single();
                    return a.Password;
                }

            }
            catch (Exception e)
            {
                
                throw e;
            }
        }

        public void delete_account(Int32 uID, string url)
        {
            try
            {
                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
                    Account a = (from u in db.Accounts
                                 where u.URL == url && u.UID == uID
                                 select u).Single();
                    db.Accounts.DeleteOnSubmit(a);
                    db.SubmitChanges();
                }

            }
            catch (Exception e)
            {

                throw e;
            }
        }



    }
}
